import React, { Component } from 'react';

//错误边界处理组件，react16以上支持
class ErrorBoundary extends Component {
    constructor(props){
        super(props)
        this.state = {
            hasError:false
        }
    }
    //错误钩子，当组件出错，会触发该钩子
    componentDidCatch(error, errorInfo) {
        this.setState({
            hasError:true
        })
        console.log(error)
    }
    render() {
        if(this.state.hasError){
            return <div>error</div>
        }
        return this.props.children;;
    }
}

export default ErrorBoundary;